package tt.dz.dao.ext.charger;

import java.sql.*;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.collections.map.ListOrderedMap;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.*;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;

import tt.dz.dao.base.BaseDao;
import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.charger.OmsCharger;
import tt.dz.util.OmsConstant;
import tt.dz.util.annotation.Table;
import tt.dz.vo.oms.ChargerExportVO;

/**
 * 电桩基础信息数据操作Dao
 * 
 * @author xielongfei
 * @modify pengyu
 * 
 */
@Repository
@Table(name = "oms_charger")
public class OmsChargerDao extends OmsCommonDao<OmsCharger> {

	
	
	
	@Override
	public int LogicDeleteObject(Object obj) {
		
		return super.LogicDeleteObject(obj);
	}

	@Override
	public List<Map<String, Object>> findDataBySql(String sql, Object... params) {
		
		return super.findDataBySql(sql, params);
	}

	@Override
	public List<OmsCharger> findDataBySqlOfClass(String sql, Class<?> cls, Object... params) {
		
		return super.findDataBySqlOfClass(sql, cls, params);
	}

	@Override
	public List<OmsCharger> findExample(Object obj, Integer pageNo, Integer pageSize, String sortName,
			Integer sortVal) {
		
		return super.findExample(obj, pageNo, pageSize, sortName, sortVal);
	}

	@Override
	public List<OmsCharger> findListByClass(Class<?> cls, ListOrderedMap paramsMap, Integer pageNo, Integer pageSize,
			String sortName, Integer sortVal) {
		
		return super.findListByClass(cls, paramsMap, pageNo, pageSize, sortName, sortVal);
	}

	@Override
	public int save(Object obj) {
		
		return super.save(obj);
	}

	@Override
	public int saveList(List<OmsCharger> obj) {
		
		return super.saveList(obj);
	}

	@Override
	public int updateObject(Object obj) {
		
		return super.updateObject(obj);
	}

	@Override
	public OmsCharger findOneExample(Object obj, String order, String sortVal) {
		
		return super.findOneExample(obj, order, sortVal);
	}

	@Override
	public List<OmsCharger> findAll(Object obj) {
		
		return super.findAll(obj);
	}

	@Override
	public JdbcTemplate getJdbcTemplate() {
		
		return super.getJdbcTemplate();
	}

	@Override
	public int hashCode() {
		
		return super.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		
		return super.equals(obj);
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		
		return super.clone();
	}

	@Override
	public String toString() {
		
		return super.toString();
	}

	@Override
	protected void finalize() throws Throwable {
		
		super.finalize();
	}

	@SuppressWarnings("rawtypes")
	public JsonForList deleteCharger(List<String> list,String oper_id) {
		JsonForList json = new JsonForList();
		StringBuilder sb =new StringBuilder("update oms_charger set valid_flag = 0,update_oper_id ='"+oper_id+"' where charger_id in ( ");
		for(String id :list){
			sb.append("'"+id+"',");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(")");
		StringBuilder state_alternate_sb = new StringBuilder("update oms_charger_state_alternate set valid_flag = 0 where charger_id in ( ");
		for(String id :list){
			state_alternate_sb.append("'"+id+"',");
		}
		state_alternate_sb.deleteCharAt(state_alternate_sb.length()-1);
		state_alternate_sb.append(")");
		StringBuilder state_direct_sb = new StringBuilder("update oms_charger_state_direct set valid_flag = 0 where charger_id in ( ");
		for(String id :list){
			state_direct_sb.append("'"+id+"',");
		}
		state_direct_sb.deleteCharAt(state_direct_sb.length()-1);
		state_direct_sb.append(")");
		try {
			this.exec(sb.toString());
			this.exec(state_alternate_sb.toString());
			this.exec(state_direct_sb.toString());
			json.setCode(OmsConstant.SuccessCodeEnum.SuccessCode.value());
			json.setMsg("loginDelCharger success");
			return json;
		} catch (Exception e) {
			json.setCode(OmsConstant.ErrorCodeEnum.SqlExecException.value());
			json.setMsg("loginDelCharger Exception");
			e.printStackTrace();
			return json;
		} 
	
	}

	public void deleteChargerForStation(String station_id, Integer directCount, Integer alternateCount) {
		
		StringBuilder sb =new StringBuilder("update oms_station set direct_num = direct_num - "+String.valueOf(directCount)+","
				                           + "alternate_num = alternate_num - " + String.valueOf(alternateCount) + ","
				                           + "total_charger = total_charger - "+String.valueOf(directCount)+" - " + String.valueOf(alternateCount)
				                           + " where station_id = '" + station_id + "'");
		this.exec(sb.toString());
	}

	public List<ChargerExportVO> exportChargers(String sql)
	{
		List<ChargerExportVO> list = getJdbcTemplate().query(sql, new BeanPropertyRowMapper<>(ChargerExportVO.class));
		return list;
	}

	public int deleteChargerByStatus(List<String> list, String oper_id, String chargerType) {
		StringBuilder sb =new StringBuilder("update oms_charger a,");
		if ("1".equals(chargerType)) {
			sb.append("oms_charger_state_direct");
		}else {
			sb.append("oms_charger_state_alternate");
		}
		 
		sb.append(" b set a.valid_flag = 0,a.update_oper_id ='"+oper_id+"' where a.charger_id in ( ");
		for(String id :list){
			sb.append("'"+id+"',");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(") and a.charger_id=b.charger_id  and (b.charge_status=0  or b.charge_status=8)");  //空闲或者离线状态才能删除
		
		int execNum = this.execAndReturnNum(sb.toString());
		return execNum;
	}

	public boolean deleteChargerPort(List<String> list, String chargerType) {
		StringBuilder sb = new StringBuilder("update ");
		if ("1".equals(chargerType)) {
			sb.append("oms_charger_state_direct");
		}else {
			sb.append("oms_charger_state_alternate");
		}
		sb.append(" set valid_flag = 0 where charger_id in ( ");
		for(String id :list){
			sb.append("'"+id+"',");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(")");
		return this.exec(sb.toString());
	}


}
